package com.ivsign.lib_idcardidentification;

import android.util.Log;

/**
 * @description :
 * @autHor :  Jason
 * @date : 2018/8/17 16:33
 */

public class IDCheckUtils {

    private static final String TAG = "utils";

    @SuppressWarnings("all")
    public static int Check_head02_end03(int len, byte[] buff) {


        if (buff[0] == 0x02) {
            if (len >= 5) {
                if (len == ((buff[1] & 0x0F) * 256 + buff[2] + 5)) {

                    if (buff[len - 1] == 0x03) {
                        if (buff[len - 2] == Verify_02head_03end(buff)) // 判断校验正确否
                        {

                            int b = HexUtil.getPacketLength(buff[3], buff[4]);
                            Log.d(TAG, b + "Check_head02_end03() called with: len = [" + len + "], buff = [" + buff + "]");
                            // memcpy (&TxBuffer[0], &RxBuffer[0],
                            // RxCounter1_buff);
                            return 0x9000;
                        }
                    }
                } else
                    return 0x6ff3;
            } else if (len > ((buff[1] & 0x0F) * 256 + buff[2] + 5)) {
                return 0x6ff2;
            }
        }
        return 0x6ff1;
    }

    public static int Check_headTripleAA(int rxcounter, byte[] buff) {

        if ((buff[0] == (byte) 0xAA) && (buff[1] == (byte) 0xAA) && (buff[2] == (byte) 0xAA) && (buff[3] == (byte) 0x96)
                && (buff[4] == 0x69)) {
            if (rxcounter >= 10) {
                if ((rxcounter - 7) == ((buff[5] & 0x0F) * 256 + buff[6])) {
                    int b = HexUtil.getPacketLength(buff[5], buff[6]);
                    Log.d(TAG, b + "Check_headTripleAA() called with: rxcounter = [" + rxcounter + "], buff = [" + buff + "]");
                    // Log.d(TAG, "Check_headTripleAA() called with: rxcounter = [" + rxcounter + "], buff = [" + buff + "]");
                    if (buff[rxcounter - 1] == Verify_Triple_AA(buff)) // 判断校验正确否
                    {
                        // memcpy (&TxBuffer[0], &RxBuffer[0], RxCounter1_buff);
                        return 0x9000;
                    } else
                        return 0x6ff3;
                }
            }
            return 0x6ff2;
        }
        return 0x6ff1;

    }

    public static int Check_headTripleFFWith6002(int rxcounter, byte[] buff) {


        //
        if ((buff[0] == (byte) 0xff) && (buff[1] == (byte) 0xff) && (buff[2] == (byte) 0xff)) {
            if (rxcounter >= 5) {
                if (rxcounter == ((buff[3] & 0x0F) * 256 + buff[4])) {
                    int b = HexUtil.getPacketLength(buff[3], buff[4]);//
                    Log.d(TAG, b + "Check_headTripleFFWith6002() called with: rxcounter = [" + rxcounter + "], buff = [" + buff + "]");
                    if ((buff[rxcounter - 2] == (byte) 0xfe) && (buff[rxcounter - 1] == (byte) 0xfe)) {
                        if (buff[rxcounter - 3] == Verify_Triple_FF(buff)) // 判断校验正确否
                        {
                            // memcpy (&TxBuffer[0], &RxBuffer[0],
                            // RxCounter1_buff);
                            return 0x9000;
                        }
                    }
                } else
                    return 0x6ff2;
            }
        }
        return 0x6ff1;
    }

    public static int Check_headTripleFFWith6013(int rxcounter, byte[] buff) {
        if ((buff[0] == (byte) 0xff) && (buff[1] == (byte) 0xff) && (buff[2] == (byte) 0xff)) {
            if (rxcounter >= 5) {
                int b = HexUtil.getPacketLength(buff[3], buff[4]);
                Log.d(TAG, b + "Check_headTripleFFWith6013() called with: rxcounter = [" + rxcounter + "], buff = [" + buff + "]");
                int a = ((buff[3] & 0x0F) * 256 + (buff[4] & 0xF0) * 16 + (buff[4] & 0x0f));
                if (rxcounter == HexUtil.getPacketLength(buff[3], buff[4])) {
                    if ((buff[rxcounter - 2] == (byte) 0xfe) && (buff[rxcounter - 1] == (byte) 0xfe)) {
                        if (buff[rxcounter - 3] == Verify_Triple_FF(buff)) // 判断校验正确否
                        {
                            // memcpy (&TxBuffer[0], &RxBuffer[0],
                            // RxCounter1_buff);
                            return 0x9000;
                        }

                        return 0x9000;
                    }
                } else
                    return 0x6ff2;
            }
        }
        return 0x6ff1;
    }

    public static int Check_headTripleFFWith6113(int rxcounter, byte[] buff) {

        if ((buff[0] == (byte) 0xff) && (buff[1] == (byte) 0xff) && (buff[2] == (byte) 0xff)) {
            if (rxcounter >= 5) {
                if (rxcounter == (HexUtil.getPacketLength(buff[3], buff[4]))) {
                    int b = HexUtil.getPacketLength(buff[3], buff[4]);
                    Log.d(TAG, b + "Check_headTripleFFWith6113() called with: rxcounter = [" + rxcounter + "], buff = [" + buff + "]");
                    if ((buff[rxcounter - 2] == (byte) 0xfe) && (buff[rxcounter - 1] == (byte) 0xfe)) {
                        if (buff[rxcounter - 3] == Verify_Triple_FF(buff)) // 判断校验正确否
                        {
                            // memcpy (&TxBuffer[0], &RxBuffer[0],
                            // RxCounter1_buff);
                            return 0x9000;
                        }

                        //return 0x9000;
                    }
                } else
                    return 0x6ff2;
            }
        }
        return 0x6ff1;
    }

    public static byte Verify_02head_03end(byte[] data_buf) {
        byte verify = 0;
        int i;
        int RxDataLength;
        RxDataLength = (data_buf[1] & 0x0F) * 256 + data_buf[2] + 5;
        for (i = 3; i < RxDataLength - 2; i++) // 校验=长数据（nB）
        {
            verify ^= data_buf[i];
        }
        return verify;
    }

    public static byte Verify_Triple_FF(byte[] data_buf) {
        byte verify = 0;
        int i;
        int RxDataLength;
        RxDataLength = (data_buf[3] & 0x0F) * 256 + data_buf[4];
        for (i = 3; i < RxDataLength - 3; i++) // 校验=长数据（nB）
        {
            verify += data_buf[i];
        }
        return verify;
    }

    public static byte Verify_Triple_AA(byte[] data_buf) {
        byte verify = 0;
        int i;
        int RxDataLength;
        RxDataLength = (data_buf[5] & 0x0F) * 256 + data_buf[6];
        for (i = 0; i < RxDataLength + 1; i++) // 校验=长数据（nB）
        {
            verify ^= data_buf[i + 5];
        }
        return verify;

    }
}
